<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            录入不合格数量
        </div>
    </header>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <ul class="aui-list aui-form-list">
            <li class="aui-list-item">
                <div class="aui-list-item-inner">
                    <div class="aui-list-item-label" style="font-size: 14px;">发货单号</div>
                    <input type="text" class="aui-input" placeholder="请 扫描/输入 发货单号" style="width: 80%;font-size: 14px;" id="consignment">
                    <i class="aui-input-addon aui-iconfont aui-icon-scan " style="font-size: 1.6rem" onclick="toScanner();"></i>
                </div>
            </li>
        </ul>
        <div style="width:100%;margin-top:2%;"></div>
        <div class="aui-btn aui-btn-info" style="width:35%;margin-left:10%;" onclick="onSelect()">查询</div>
        <div class="aui-btn aui-btn-info" style="width:35%;margin-left:10%;" onclick="updateQty()">修改数量</div>
        <!-- <div class="aui-btn aui-btn-info" style="width:25%;margin-left:5%;" onclick="toPrint()">补打条码</div> -->
        <div style="background-color:white; height: 100%;width: 100%;margin-top:2%;">
            <table class="table" style='width:100%;' id="table">
                <thead>
                    <tr style="text-align: center;background-color: #DCE9F2;color: #467aa7;font-size: 14px;font-weight: bold;">
                        <td></td>
                        <!-- <td algin='center'>序号</td> -->
                        <td algin='center'>箱号</td>
                        <td algin='center'>物料号</td>
                        <td algin='center'>数量</td>
                        <td algin='center'>不合格数</td>
                        <td algin='center'>状态</td>
                        <td style="display:none;">条码</td>
                        <td style="display:none;">发货单号</td>
                        <td style="display:none;">id</td>
                    </tr>
                </thead>
                <tbody id="tbodyHtml">
                </tbody>
            </table>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">
    //扫描发货单号和箱号
    function toScanner() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function(ret, err) {
            var sm = ret;
            var text = document.getElementById('consignment');

            //扫发货单，查询明细
            text.value = sm;
            onChangeConsignment();

        });
    }

    //查询
    function onSelect() {
        //取发货单号
        var consignment = $("#consignment").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            $api.html($api.byId("tbodyHtml"), "");
            api.toast({
                msg: '请扫描/录入发货单！',
                duration: 3000
            });

            //不为空，查询表格数据
        } else {
            searchDetails(consignment);
        }
    }

    //箱号监听回车事件
    $("#consignment").on('keypress', function(e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {

            $("#consignment").val(tm);
            //箱号值改变
            onChangeConsignment();
        }
    });

    //发货单号值改变
    function onChangeConsignment() {
        //取发货单号
        var consignment = $("#consignment").val();
        //发货单为空，清空表格信息
        if (consignment == "" || consignment == null || consignment == undefined) {
            $api.html($api.byId("tbodyHtml"), "");

            //不为空，查询表格数据
        } else {
            searchDetails(consignment);
        }
    }

    //根据发货单查询表格数据
    function searchDetails(consignment) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getConsignmentBadQty";
        var data = {
            values: {
                consignment: consignment,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 30,
            returnAll: false,
            data: data
        }, function(ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");

                //返回成功
                if (myobj.code == "0") {
                    var list = eval(myobj.list);
                    var html = "";
                    for (var i = 0; i < list.length; i++) {

                        //只可以修改未退货的数据
                        if (list[i].gyStatus != "4") {
                            html += "<tr style='text-align: center;color: black;font-size: 12px;background-color: #F5F5F5;'>";
                            html += "<td><input name='sub' type='checkbox' /></td>";
                            // html += "<td name='seqNo'>" + list[i].seqNo + "</td>";
                            html += "<td name='boxnum'>" + list[i].boxnum + "</td>";
                            html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                            html += "<td name='qty'>" + list[i].qty + "</td>";
                            html += "<td name='badQty'>" + list[i].baqQty + "</td>";
                            html += "<td name='gyStatusName'>" + list[i].gyStatusName + "</td>";
                            html += "<td name='tm' style='display:none;'>" + list[i].tm + "</td>";
                            html += "<td name='consignment' style='display:none;'>" + consignment + "</td>";
                            html += "<td name='id' style='display:none;'>" + list[i].id + "</td>";
                            html += "</tr>";
                        }
                    }
                    $api.html($api.byId("tbodyHtml"), html);

                    //失败，存在没有订单数据
                } else if (myobj.code == "1") {

                    //清空表格数据
                    $api.html($api.byId("tbodyHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }

            }
        });
    }

    //修改数量
    function updateQty() {
        //选中数据
        var check = $("input[type='checkbox']:checked"); //在table中找input下类型为checkbox属性为选中状态的数据
        var count = 0;
        check.each(function() {
            count++;
        });

        //选中行不为1
        if (count != 1) {
            api.toast({
                msg: '请选择一条表格数据！',
                duration: 3000
            });
            return;
        }

        var id = "";
        var boxnum = "";
        var mrlCode = "";
        var qty = "";
        var badQty = "0";
        var consignment = "";
        check.each(function() { //遍历
            var row = $(this).parent("td").parent("tr");
            id = row.find("[name='id']").html(); //注意html()和val()
            boxnum = row.find("[name='boxnum']").html();
            mrlCode = row.find("[name='mrlCode']").html();
            qty = row.find("[name='qty']").html();
            consignment = row.find("[name='consignment']").html();
        });

        api.openWin({
            name: 'uwmYqgyReceipt_badQty_input',
            url: 'uwmYqgyReceipt_badQty_input.html',
            pageParam: {
                id: id,
                boxnum: boxnum,
                mrlCode: mrlCode,
                qty: qty,
                badQty: badQty,
                consignment: consignment
            }
        });
    }

    //页面刷新
    function refresh(consignment) {
        searchDetails(consignment);
    }

    //订单返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>
